// 加载Express模块
const express = require("express");
// 加载MySQL模块
const mysql = require("mysql");
// 加载bodyParser模块
const bodyParser = require("body-parser");
// 加载MD5模块
const md5 = require("md5");
const qs = require("querystring");
const https = require("https");
// 创建服务器对象
const server = express();
//指定静态目录
server.use(express.static("public"));
// 设置监听接口
// server.listen(3306,()=>{
//     console.log("服务器运行成功")
// })

//配置连接池
const pool = require("./pool.js");
//post传参
server.use(
  bodyParser.urlencoded({
    extended: false,
  })
);
  // console.log(result);
// 加载CORS模块
const cors = require("cors");
const { post } = require("request");
// 使用CORS中间件
server.use(
  cors({
    origin: ["http://localhost:8080", "http://127.0.0.1:8080",
    "http://localhost:8081", "http://127.0.0.1:8081",
    "http://localhost:8082", "http://127.0.0.1:8082",],
  })
);

// 手机短信验证 引入
const { randomCode, sendCode } = require("../miapp/src/utils/getMessage");
const { getConnection } = require("./pool.js");
const { send } = require("process");
const servers = server.listen(4200, function () {
  const { address, port } = servers.address();
  console.log(address, port);
  console.log("HTTP服务已启动");
});


// 手机短信验证 注册 接口
// 手机短信验证 注册 手机号查重 接口
server.get('/iphoneblur',(req,res)=>{
  let iphone=req.query.iphone;
  let sql='SELECT COUNT(uid) AS count FROM yb_user WHERE iphone=?';
  pool.query(sql,[iphone],(error,results)=>{
    if (error) throw error;
    let count = results[0].count;
    if(count==0){
      res.send({massage:'手机号未注册,可以使用',code:200})
    }else{
      res.send({massage:'手机号已被注册',code:201})
    }
  })
})
  // 手机注册 接口
server.post('/iphonerecord', (req, res) => {
  let iphone=req.body.iphone;
  let upwd=req.body.upwd;
  let sql = 'SELECT COUNT(uid) AS count FROM yb_user WHERE iphone=?'
  pool.query(sql, [iphone], (error, results) => {
      if (error) throw error;
      let count = results[0].count;
      if (count == 0) {
          sql = 'INSERT yb_user(iphone,upwd) VALUES(?,?)'
          pool.query(sql,[iphone,upwd], (error, resultsA) => {
              if (error) throw error;
              // console.log(resultsA)
              res.send({ massage: '注册成功', code: 200,resultsA:resultsA})
          })
      } else {
          res.send({ message: '手机号已被注册', code: 201})
      }
  })
})
  // 手机号 登录 接口
  server.post("/iphonelogin",(req,res)=>{
    let iphone=req.body.iphone;
    let upwd=req.body.upwd;
    let sql='SELECT * FROM yb_user WHERE iphone=? and upwd=?';
    pool.query(sql,[iphone,upwd],(error,results)=>{
      if(error) throw error;
      let count = results;
      if(count==0){
        res.send({message:'登陆失败',code:201})
      }else{
        res.send({message:'登录成功',code:200,results:results})
      }
    })
  })
// 手机短信验证 接口
server.get("/sms",(req,res)=>{
    let iphone=req.query.iphone
    let code=randomCode(6);//生成6位数字随机验证码
    sendCode(iphone,code,function(success){
        if(success){
            res.send({message:code,code:200});
        }else{
            res.send("短信验证码发送失败");
        }
    })
  })
